//
//  PlayWarGame.m
//  WarGame
//
//  Created by iD Student on 6/27/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PlayWarGame.h"
#import "WarGameDecks.h"
@interface PlayWarGame()
@property (weak, nonatomic) IBOutlet UILabel *botDisplay;
@property (weak, nonatomic) IBOutlet UILabel *playerDisplay;
@property (weak, nonatomic) IBOutlet UIButton *flipContinue;
@property (weak, nonatomic) IBOutlet UILabel *resultDisplay;

@property (strong, nonatomic) NSDictionary *conversionTable;
@property (strong, nonatomic) NSString *mode;
@property (strong, nonatomic) WarGameDecks *cards;
@end

@implementation PlayWarGame
@synthesize botDisplay = _botDisplay;
@synthesize playerDisplay = _playerDisplay;
@synthesize flipContinue = _flipContinue;
@synthesize resultDisplay = _resultDisplay;

@synthesize mode = _mode;
@synthesize cards = _cards;
@synthesize conversionTable = _conversionTable;
-(NSDictionary *) conversionTable
{
    if(!_conversionTable)
    {
        //create the dictionary
        _conversionTable = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"A ♥", @"0", @"2 ♥", @"1", @"3 ♥", @"2", @"4 ♥", @"3", @"5 ♥", @"4", @"6 ♥", @"5", @"7 ♥", @"6", @"8 ♥", @"7", @"9 ♥", @"8", @"10 ♥", @"9", @"J ♥", @"10", @"Q ♥", @"11", @"K ♥", @"12", @"A ♠", @"13", @"2 ♠", @"14", @"3 ♠", @"15", @"4 ♠", @"16", @"5 ♠", @"17", @"6 ♠", @"18", @"7 ♠", @"19", @"8 ♠", @"20", @"9 ♠", @"21", @"10 ♠", @"22", @"J ♠", @"23", @"Q ♠", @"24", @"K ♠", @"25", @"A ♦", @"26", @"2 ♦", @"27", @"3 ♦", @"28", @"4 ♦", @"29", @"5 ♦", @"30", @"6 ♦", @"31", @"7 ♦", @"32", @"8 ♦", @"33", @"9 ♦", @"34", @"10 ♦", @"35", @"J ♦", @"36", @"Q ♦", @"37", @"K ♦", @"38",@"A ♣", @"39", @"2 ♣", @"40", @"3 ♣", @"41", @"4 ♣", @"42", @"5 ♣", @"43", @"6 ♣", @"44", @"7 ♣", @"45", @"8 ♣", @"46", @"9 ♣", @"47", @"10 ♣", @"48", @"J ♣", @"49", @"Q ♣", @"50", @"K ♣", @"51", nil];
        
    }
    return _conversionTable;
}


- (IBAction)flip
{
    if(self.mode == @"flip")
    {
        // show the top card of both decks
        
        int bcard = [self.cards pullFromTopOfBotDeck];
        int pcard = [self.cards pullFromTopOfPlayerDeck];
        
        NSString *bStr = [self.conversionTable objectForKey:[NSString stringWithFormat:@"%d",bcard]];
        NSString *pStr = [self.conversionTable objectForKey:[NSString stringWithFormat:@"%d",pcard]];
        
        [self.botDisplay setText:bStr];
        [self.playerDisplay setText:pStr];
        
        // check to see who won
        if ( (pcard+12 % 13 ) > ( bcard+12 % 13 ) )
        {
        [self.resultDisplay setText:@"VICTORY!"];
            [self.resultDisplay setTextColor:[UIColor colorWithRed:0.5f green:0 blue:0.5f alpha:1.0f]]; 
            [self.cards putOnBottomOfPlayerDeck:bcard];
            [self.cards putOnBottomOfPlayerDeck:pcard];
            
        }
        else 
        {
        [self.resultDisplay setText:@"DEFETED!"];   
            [self.resultDisplay setTextColor:[UIColor colorWithRed:1.0f green:0 blue:0.1f alpha:1.0f]]; 
            [self.cards putOnBottomOfBotDeck:bcard];
            [self.cards putOnBottomOfBotDeck:pcard];
        }
             
        
        // set the play mode
        self.mode = @"continue";
    }
    else
    {
        [self.flipContinue setTitle:@"FLIP" forState:UIControlStateNormal];
        int bcards = [self.cards getBotDeckSize];
        int pcards = [self.cards getPlayerDeckSize];
        [self.botDisplay setText:[NSString stringWithFormat:@"%@ cards", [[NSNumber numberWithInt:bcards] stringValue]]];
        [self.playerDisplay setText:[NSString stringWithFormat:@"%@ cards", [[NSNumber numberWithInt:pcards] stringValue]]]; 
        [self.resultDisplay setText:@""];
        self.mode = @"flip";
        
        
    }
}
-(void)viewDidLoad
{

    
    [super viewDidLoad];
    self.cards = [[WarGameDecks alloc] init];
    [self.cards shuffleFullDeck];
    
    self.mode = @"flip";
    [self.flipContinue setTitle:@"FLIP" forState:UIControlStateNormal];
    int bcards = [self.cards getBotDeckSize];
    int pcards = [self.cards getPlayerDeckSize];
    [self.botDisplay setText:[NSString stringWithFormat:@"%@ cards", [[NSNumber numberWithInt:bcards] stringValue]]];
    [self.playerDisplay setText:[NSString stringWithFormat:@"%@ cards", [[NSNumber numberWithInt:pcards] stringValue]]];

    
}
- (void)viewDidUnload {
    [self setBotDisplay:nil];
    [self setPlayerDisplay:nil];
    [self setFlipContinue:nil];
    [self setResultDisplay:nil];
    [self setResultDisplay:nil];
    [self setResultDisplay:nil];
    [super viewDidUnload];
}
@end
